package acwing.bfs;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * 地牢大师
 *
 * @author yang
 * @date 2021/12/8
 */
public class 地牢大师 {

    static int N = 110;
    static int L;
    static int R;
    static int C;
    static PIIs start;
    static PIIs end;
    static char[][][] g = new char[N][N][N];
    static int[][][] dist = new int[N][N][N];
    // 走的相对坐标
    static int[] dx = new int[] {1,-1,0,0,0,0};
    static int[] dy = new int[] {0,0,1,-1,0,0};
    static int[] dz = new int[] {0,0,0,0,1,-1};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            L = sc.nextInt();
            R = sc.nextInt();
            C = sc.nextInt();
            if (L == 0 && R == 0 && C == 0) break;
            for (int i = 0; i < L; i++) {
                for (int j = 0; j < R; j++) {
                    char[] chars = sc.next().toCharArray();
                    for (int k = 0; k < C; k++) {
                        g[i][j][k] = chars[k];if(g[i][j][k] == 'S') start = new PIIs(i,j,k);
                        if(g[i][j][k] == 'E') end = new PIIs(i,j,k);
                    }
                }
            }
            int dis = bfs();
            if(dis == -1) System.out.println("Trapped!");
            else System.out.println("Escaped in " + dis + " minute(s).");
        }

    }

    private static int bfs() {
        Queue<PIIs> q = new LinkedList<>();
        q.add(start);
        for (int i = 0; i < L; i++) {
            for (int j = 0; j < R; j++) {
                Arrays.fill(dist[i][j], -1);
            }
        }
        dist[start.x][start.y][start.z] = 0;
        while (!q.isEmpty()) {
            PIIs t = q.poll();
            for (int i = 0; i < 6; i++) {
                int x = t.x + dx[i];
                int y = t.y + dy[i];
                int z = t.z + dz[i];
                if (x < 0 || x >= L || y < 0 || y >= R || z < 0 || z >= C) continue;
                if (g[x][y][z] == '#') continue;
                if (dist[x][y][z] != -1) continue;

                dist[x][y][z] = dist[t.x][t.y][t.z] + 1;
                if (x == end.x && y == end.y && z == end.z) return dist[x][y][z];
                q.add(new PIIs(x, y, z));
            }
        }
        return -1;
    }

}
class PIIs {

    public int x;
    public int y;
    public int z;

    public PIIs(int x, int y, int z) {
        this.x = x; // 层
        this.y = y; // 行
        this.z = z; // 列
    }
}
